package shiyan6;

import java.util.concurrent.atomic.AtomicInteger;

public class BreadProducer extends Thread {

    private static final AtomicInteger COUNTER = new AtomicInteger(1);
    private static final long SLEEP_MILLIS = 80L;

    private final YiXinZhiShop shop;
    private final int productionCount;

    public BreadProducer(String name, YiXinZhiShop shop, int productionCount) {
        super(name);
        this.shop = shop;
        this.productionCount = productionCount;
    }

    @Override
    public void run() {
        for (int i = 0; i < productionCount; i++) {
            Bread bread = new Bread(COUNTER.getAndIncrement());
            shop.produceBread(bread);
            try {
                Thread.sleep(SLEEP_MILLIS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println(getName() + " 被中断: " + e.getMessage());
                break;
            }
        }
    }
}
